const axios = require('axios');
const iconv = require('iconv-lite');
const cheerio = require('cheerio');
const download = require('download');
const fs = require('fs');

let html = fs.readFileSync(__dirname + '/goods.html');

const $ = cheerio.load(html);

let goods = [];

$('.gl-warp li').each((index, item) => {
    let obj = {};

    //封面图
    let img = $('.gl-warp li .p-img img').eq(index)[0]?.attribs['src'];
    if(!img) return;
    obj.remote_img = 'http:' + img;
    //获取标题
    obj.title = $('.gl-warp li .p-name a em').eq(index).html();
    //获取价格
    obj.price = $('.gl-warp li .p-price i').eq(index).html();
    //店铺
    obj.shop = $('.gl-warp li .p-shop a').eq(index).html();
    //商品京东地址
    obj.url = 'https:' + $('.gl-warp li .p-img a').eq(index).attr('href');
    //是否自营
    obj.isZY = $('.gl-warp li .p-icons i').eq(index).html() === '自营' ? true : false;
    //下载图片
    //下载图片
    let dir = __dirname + '/../../public/images/goods'
    let name = obj.remote_img.split('/').pop();
    //下载图片
    let path = dir + '/'+ name;
    //下载图片
    download(obj.remote_img).pipe(fs.createWriteStream(path));
    obj.img = '/images/goods/'+name;
    //压缩进数组
    goods.push(obj);
});

fs.writeFileSync(__dirname + '/goods.json', JSON.stringify({goods}));

// const {goods} = require('./goods.json');

// let newArr = goods.map(item => {
    
// })

// fs.writeFileSync(__dirname + '/goods_2.json', JSON.stringify({goods:newArr}));




